package com.tian.common.util;



import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
 * @author zhump
 * @date 2024-09-10
 */
public class SerializedObjectUtils {


    /**
     * <pre>
     *     反序列化工具类
     * </pre>
     * @param vars 二进制数据
     * @return
     * @param <T>
     */
    public  static <T> T serializedObject(byte[] vars){
        ByteArrayInputStream bais = null;
        ObjectInputStream ois = null;
        try {
            bais = new ByteArrayInputStream(vars);
            ois = new ObjectInputStream(bais);
            T t = (T) ois.readObject();
            return t;
        }catch (IOException | ClassNotFoundException e ){
            System.out.println("serializedObject exception:"+e);
            return null;
        } finally {
            try {
                if (bais != null){
                    bais.close();
                }
                if (ois != null){
                    ois.close();
                }
            }catch (IOException e){
                System.out.println("serializedObject close exception:"+e);
                return null;
            }
        }
    }
}
